﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using BUS;

namespace GUI
{
    public partial class MainForm : Form
    {
        private int nhomtin;
        private bool viewAll = true;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            loadGrid();
            loadNhomTin();
        }

        private void loadNhomTin()
        {
            BindingList<NhomTin> list = new NhomTinBUS().LoadAll();
            //hien thi len combobox
            cbxNhomTin.DataSource = list;
            cbxNhomTin.DisplayMember = "TenNhomTin";
            cbxNhomTin.ValueMember = "MaNhomTin";
            cbxNhomTin.SelectedItem = null;
        }
        private void loadGrid() {
            BindingList<TinRaoVat> list;
            if(viewAll)
                list = new TinRaoVatBUS().LoadAll();
            else
                list = new TinRaoVatBUS().LoadAllByNhomTin(nhomtin);
            //hien thi len combobox
            gridTinRaoVat.DataSource = list;
            gridTinRaoVat.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            int totalCol = gridTinRaoVat.Columns.Count;
            for (int each = 0; each < totalCol; each++)
            {
                gridTinRaoVat.Columns[each].Visible = false;
            }
            gridTinRaoVat.Columns["TieuDe"].Visible = true;
            gridTinRaoVat.Columns["TieuDe"].HeaderText = "Tiêu đề";
            gridTinRaoVat.Columns["TieuDe"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            gridTinRaoVat.Columns["NgayDang"].Visible = true;
            gridTinRaoVat.Columns["NgayDang"].HeaderText = "Ngày đăng";

            gridTinRaoVat.Columns["NgayKetThuc"].Visible = true;
            gridTinRaoVat.Columns["NgayKetThuc"].HeaderText = "Ngày hết hạn";
           
        }
        private void gridTinRaoVat_DoubleClick(object sender, EventArgs e)
        {
            BindingList<TinRaoVat> list = gridTinRaoVat.DataSource as BindingList<TinRaoVat>;
            int index = gridTinRaoVat.SelectedRows[0].Index;
            TinRaoVat selected = list[index];
            ChiTietTinRaoVatForm chitiet = new ChiTietTinRaoVatForm();
            chitiet.MaTinRaoVat = selected.MaTin;
            chitiet.Show();

        }

        private void chkViewAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkViewAll.Checked)
            {
                cbxNhomTin.Enabled = false;
                cbxNhomTin.SelectedItem = null;
                viewAll = true;
                loadGrid();
            }
            else {
                cbxNhomTin.Enabled = true;
                cbxNhomTin.SelectedIndex = 0;
                viewAll = false;
                loadGrid();
            }
        }

        private void cbxNhomTin_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!chkViewAll.Checked)
            {
                viewAll = false;
                nhomtin = int.Parse(cbxNhomTin.SelectedValue.ToString());
                loadGrid();
            }
        }

        
    }
}
